package cn.edu.ctbu.datajpa.webapi;


import cn.edu.ctbu.datajpa.helper.PageUtils;
import cn.edu.ctbu.datajpa.domain.Teacher;

import cn.edu.ctbu.datajpa.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/webapi/teacher")
public class TeacherRestController {

    @Autowired
    public TeacherService teacherService;

    @GetMapping("/LIST")
    public List<Teacher> getAll(){

        List<Teacher> teachers=teacherService.findAll();
        return teachers;

    }

    /**
     * 分页读取教师表的全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "name",defaultValue = "") String name){

        //按id排倒序
        Sort sort=Sort.by(Sort.Direction.ASC,"id");

        Page<Teacher> teacherPage;

        if(StringUtils.isEmpty(name))
        {
            Pageable pageable= PageRequest.of(page,size,sort);
            teacherPage=teacherService.findAll(pageable);
        }

        else
        {
            Teacher teacher=new Teacher();
            teacher.setTname(name);

            Pageable pageable=PageRequest.of(page,size,sort);

            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Teacher> example=Example.of(teacher,matcher);

            teacherPage=teacherService.findAll(example,pageable);
        }

        PageUtils pageUtils=new PageUtils(teacherPage.getContent(),teacherPage.getTotalElements());

        return pageUtils;
    }

    /**
     * 新增教师信息的方法 /webapi/teacher/insert
     * @param teacher
     * @return
     */
    @PostMapping("/insert")
    public Teacher insert(Teacher teacher){

        Teacher teacher1=teacherService.insert(teacher);

        return teacher1;

    }

    /**
     * 更新教师信息的方法 /webapi/teacher/update
     * @param teacher
     * @return
     */
    @PutMapping("/update")
    public Teacher update(Teacher teacher){

        Teacher oldteacher=teacherService.getById(teacher.getId()); //读取旧的数据
        if(StringUtils.isEmpty(teacher.getPassword())){

            //没有新密码，就用旧的密码
            teacher.setPassword(oldteacher.getPassword());

        }

        teacher=teacherService.update(teacher);
        return teacher;

    }

    /**
     * 删除教师的方法 /webapi/teacher/delete
     *
     * @param id
     */
    @DeleteMapping ("/delete/{id}")
    public void delete(@PathVariable Integer id){

        teacherService.delete(id);

    }

    /**
     * 读取的url；/webapi/teacher/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public Teacher get(@PathVariable Integer id){

        Teacher teacher=teacherService.getById(id);
        //teacher.setPassword("");
        return teacher;

    }

}
